#include <stdio.h>
#include <string.h>
#include <stdlib.h>  //strtol
#include <stdbool.h>
#include <assert.h>
#include <argp.h>

/*
  I believe that I found a bug
  echo "Probably the simplest case is to decrypt a short sentence." | ./caesar -s 4 | ./caesar -s 22
  jrobably the simplest case is to decrypt a short sentence.
  ��[joshua@dobby c]$ 

*/

#define DEFAULT_SHIFT 1
#define A_CHAR 65
#define Z_CHAR 90
#define A_LOWER_CHAR 97
#define Z_LOWER_CHAR 122

/* This program opens a file outputs the file in a caesar cipher. */
char fileName [128];
short int shift = DEFAULT_SHIFT;

const char * argp_program_version = "0.1";
const char * argp_program_bug_address = "jbranso@fastmail.com";

error_t argp_err_exit_status;

static const struct argp_option options [] =
  {
   {"file" , 'f', "FILE", 0, "Output the caesar cipher of a file." },
   {"shift", 's', "INT", 0, "Specify the shift of the cipher." },
   { 0 }
  };

//define an argp parse function
error_t argp_parser (int opt, char *arg, struct argp_state *state)
{
  extern char fileName [];
  extern short int shift;
  switch (opt)
    {
      // if this parser function is called on an option that it doesn't recognize, then don't do anything.
    default:
      return ARGP_ERR_UNKNOWN;
    case 'f':
      {
        //printf ("file is %s\n", arg);
        memcpy (fileName, arg, strlen (arg));
        break;
      }
    case 's':
      {
        shift = (int) strtol (arg, NULL, 0);
        if (shift < 0)
          shift = abs (shift);
        else if (shift > 26)
          shift = shift % 26;
        break;
      }
    }
  return 0;
}

struct argp argp =
  {
   options, argp_parser, 0,
   "A simple program implementing a caesar cipher.\nThe default shift is 1."
  };
/* a string containing the basic usage of this program. */

/* This decrypts letters.
   print_shift('a', 1) -> 'b'
   print_shift('B', 1) -> 'C'
*/
void print_shifted (char c)
{
  extern short int shift;
  short int upper;
  short int lower;
  if (isupper (c))
    {
      lower = A_CHAR;
      upper = Z_CHAR;
    }
  else if (islower(c))
    {
      lower = A_LOWER_CHAR;
      upper = Z_LOWER_CHAR;
    }
  else
    {
      assert(-1);
    }
  // if we add the "corrective" shift, and the resulting char is not
  // a letter, then we need to do tweak things.
  if (((int) c + shift) > upper)
    {
      int subn = upper - (int) c;
      int newShift = shift - (subn + shift);
      c = (char) (lower + newShift);
      putchar (c);
    }
  else
    {
      putchar ((char) ((int) c + shift));
    }
}

void encrypt (int shift, FILE * stream)
{
  char c;
  while ((c = getc(stream)) != EOF)
    {
      //if the char is a number or punctionation, etc, print it.
      if (!(isalpha ((int) c)))
        putchar(c);
      else
        print_shifted(c);
    }
}

int main (int argc, char **argv) {
  extern char fileName [];
  extern short int shift;
  argp_parse (&argp, argc, argv, 0, 0, 0);
  FILE * stream;
  if ((stream = fopen (fileName, "r")) == NULL)
    {
      stream = stdin;
    }
  encrypt (shift, stream);
  fclose (stream);
  return 0;
}
